<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/19
 * Time: 15:10
 */
namespace app\business\controller;

use think\Db;
use think\Session;

class Index extends Common
{
	/**
	 * 判断是否登录
	*/
	public function _initialize()
	{
		parent::_initialize();
	}
	
	/**
	 * 商家个人信息
	 * @param string get.ordernumber 要检索的订单号
	 * @param string get.status 需要处理的订单
	*/
	public function index()
	{
		$where = ['query'=>[]];
		$db = Db::table('business')
				->field('purchase.ordernumber,purchase.date,business.id,shop.address')
				->join('purchase','business.id = purchase.bid','LEFT')
				->join('shop','shop.id = purchase.sid','LEFT')
				->order('purchase.ordernumber desc')
				->group('purchase.ordernumber');
		if(input('get.status')){
			$db->where('purchase.status',2);
			$where['query']['status'] = input('get.status');
		}else{
			$db->whereNotIn('purchase.status',[0,1]);
		}
		if(!empty(input('get.ordernumber'))){
			$ordernumber = trim(input('get.ordernumber'));
			$db->where('purchase.ordernumber',$ordernumber);
			$where['query']['ordernumber'] = $ordernumber;
		}
		$result = $db->group('purchase.ordernumber')
			->where('purchase.bid',Session::get('business.id'))
			->paginate(8,false,$where);
		return view('index',compact('result','ordernumber'));
	}
	
	/**
	 * 详情
	 * @param string get.ordernumber 订单号
	 * @param string get.bid 商家id
	*/
	public function detail()
	{
		$bid = input('get.bid');
		$ordernumber = input('get.ordernumber');
		$result = Db::table('purchase')
			->field('product.pname,product.purchaseprice,purchase.number,purchase.describe,
					purchase.total,purchase.status,purchase.id,purchase.express,purchase.oddnumber,
					purchase.images')
			->join('product','product.id = purchase.pid')
			->where('purchase.ordernumber',$ordernumber)
			->where('purchase.bid',$bid)
			->paginate(8);
		return view('detail',compact('result'));
	}
	
	/**
	 * 订单详情
	 * @param string get.id 订单详情
	*/
	public function orderdetail()
	{
		$id = input('get.id');
		$result = Db::table('purchase')
				->field('product.pname,product.category,product.introduce,
						product.purchaseprice,product.image,purchase.ordernumber,purchase.date,
						purchase.number,purchase.total,purchase.images,purchase.describe')
				->join('shop','shop.id = purchase.sid','LEFT')
				->join('product','product.id = purchase.pid','LEFT')
				->where('purchase.id',$id)
				->where('purchase.bid',Session::get('business.id'))
				->find();
		$result['image'] = json_decode($result['image']);
		return view('orderdetail',compact('result'));
	}
	
	/**
	 * 描述
	 * @param string get.id 采购单id
	*/
	public function describe()
	{
		$id = input('get.id');
		$describe = Db::table('purchase')->where('id',$id)->value('describe');
		return view('describe',compact('describe'));
	}
	
	/**
	 * @发货操作
	 * @param string get.id 采购单id
	*/
	public function express()
	{
		$id = input('get.id');
		return view('express',compact('id'));
	}
	
	/**
	 * 添加运单号
	 * @param string post.id 采购单id
	 * @param string post.oddnumber 运单号
	 * @param string post.express 快递公司
	*/
	public function logistics()
	{
		if(request()->isPost()){
			try{
				$id = input('post.id');
				$input['oddnumber'] = input('post.oddnumber');
				$input['express'] = input('post.express');
				$input['status'] = 3;
				$result = Db::table('purchase')->where('id',$id)->update($input);
				if(!$result){
					throw new \PDOException('添加失败');
				}
				$success = ['success'=>'添加成功'];
				return json($success);
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
}